//
//  NDCommonActionItem.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/30.
//

import SwiftUI

enum CommonActionType {
    case textField
    case button
    case address
    
    // 通过 jobfy 字符串初始化
    init(value: String?) {
        switch value {
        case "mnesan":
            self = .textField
        case "felicitarian":
            self = .button
        case "tropfastform":
            self = .address
        default:
            self = .textField // 默认值
        }
    }
}

enum KeboardType {
    case `default`
    case numPad
    
    init(value: String?) {
        switch value {
        case "0":
            self = .default
        case "1":
            self = .numPad
        default:
            self = .default // 默认值
        }
    }
}

struct NDCommonActionItem: View {
    let title: String
    @Binding var textContent: String
    let actionType: CommonActionType
    let plactHolder: String
    let keboardType: KeboardType
    let fieldId: Int
    let onBeginEditing: (Int) -> Void
    let onEndEditing: () -> Void
    let onClickButton: () -> Void
    
    @State private var isEditing = false
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text(title)
                .font(.system(size: 14))
                .foregroundColor(Color(hex: "#666666"))
            
            Group {
                if actionType == .textField {
                    TextField(plactHolder, text: $textContent, onEditingChanged: { editing in
                        isEditing = editing
                        if editing {
                            onBeginEditing(fieldId)
                        } else {
                            onEndEditing()
                        }
                    })
                    .padding(.leading, 5)
                    .keyboardType(keboardType == .default ? .default : .numberPad)
                } else if actionType == .button {
                    ZStack(alignment: .trailing) {
                        PlaceholderText(text: textContent,
                                        placeholder: plactHolder)
                        .frame(maxWidth: .infinity)
                        Image(systemName: "arrow.right")
                            .foregroundColor(Color(hex: "#271F24"))
                            .padding(.trailing, 16)
                    }
                    .contentShape(Rectangle())
                    .onTapGesture {
                        onClickButton()
                    }
                } else {
                    ZStack(alignment: .trailing) {
                        PlaceholderText(text: textContent,
                                        placeholder: plactHolder)
                        .frame(maxWidth: .infinity)
                        Image("addres_arrow")
                            .frame(width: 16, height: 16)
                            .scaledToFit()
                            .padding(.trailing, 16)
                    }
                    .contentShape(Rectangle())
                    .onTapGesture {
                        onClickButton()
                    }
                }
            }
            .frame(height: 48)
            .font(.system(size: 14))
            .foregroundColor(Color(hex: "#271F24"))
            .background(Color(hex: "#F7F8F9"))
            .cornerRadius(8)
        }
    }
}

